package br.ufal.ic.fracao.especialist.infra;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import br.ufal.ic.fracao.especialist.domain.Item;
import br.ufal.ic.fracao.especialist.domain.Rule;

public class Persistence {
    
    private static final String XML_FILE_PATH = "C:\\rules.xml";
	//private static final String XML_FILE_PATH = "/home/badu/Workspace/Fracao-core/rules.xml";
	//private static final String XML_FILE_PATH = "/opt/tomcat/webapps/.rules.xml";
	//private static final String XML_FILE_PATH = "/usr/share/tomcat/webapps/.rules.xml";
    private Element rules = new Element("regras");
    private Element rule;
    private Element name;
    private Element certaintyFactor;
    private Element premisse;
    private Element conclusion;
    private Element description;
    private Element value;
    private Document doc;
    
    public Persistence() {
        doc = new Document(this.rules);
    }
    
    private void saveDocument(Document doc) throws IOException {
        FileOutputStream xmlFile = new FileOutputStream(XML_FILE_PATH);
        XMLOutputter xmlOut = new XMLOutputter();
        
        xmlOut.output(doc, xmlFile);
        xmlFile.flush();
        xmlFile.close();
    }
    
    public void insertRule(Rule _rule) throws IOException, JDOMException {
        SAXBuilder builder = new SAXBuilder();
        
        this.doc = builder.build(new File(XML_FILE_PATH));
        this.rule = new Element("regra");
        this.name = new Element("nome");
        this.name.setText(_rule.getName());
        rule.addContent(name);
        
        for(Item item: _rule.getPremisses()){
            premisse = new Element("premissa");
            value = new Element("valor");
            description = new Element("descricao");
            certaintyFactor = new Element("FC");
            
            value.setText(item.getValue().toString());
            description.setText(item.getDescription());
            certaintyFactor.setText(item.getCertaintyFactor().toString());
            
            premisse.addContent(value);
            premisse.addContent(description);
            premisse.addContent(certaintyFactor);
            
            rule.addContent(premisse);
        }
        
        conclusion = new Element("conclusao");
        value = new Element("valor");
        description = new Element("descricao");
        certaintyFactor = new Element("FC");
        
        value.setText(_rule.getConclusion().getValue());
        description.setText(_rule.getConclusion().getDescription());
        certaintyFactor.setText(_rule.getConclusion().getCertaintyFactor().toString());
        
        conclusion.addContent(value);
        conclusion.addContent(description);
        conclusion.addContent(certaintyFactor);
        
        rule.addContent(conclusion);
        
        this.doc.getRootElement().addContent(this.rule);
        saveDocument(this.doc);
    }
    
    @SuppressWarnings("unchecked")
	public List<Rule> getRules() throws IOException, JDOMException{
        List<Rule> ruleList = new ArrayList<Rule>();
        SAXBuilder builder = new SAXBuilder();
        
        this.doc = builder.build(new File(XML_FILE_PATH));
        List<Element> ruleElements = this.doc.getRootElement().getChildren("regra");
        
        for (Element ruleElement : ruleElements) {
            Rule theRule = new Rule();
            theRule.setName(ruleElement.getChild("nome").getText());
            List<Element> premisseElements = ruleElement.getChildren("premissa");
            
            for (Element premisseElement : premisseElements) {
                Item premisse = new Item();
                
                premisse.setValue(premisseElement.getChild("valor").getText());
                premisse.setDescription(premisseElement.getChild("descricao").getText());
                premisse.setCertaintyFactor(Double.parseDouble(premisseElement.getChild("FC").getText()));
                
                theRule.addPremisse(premisse);
            }
            
            Item conclusion = new Item();
            
            conclusion.setValue(ruleElement.getChild("conclusao").getChild("valor").getText());
            conclusion.setDescription(ruleElement.getChild("conclusao").getChild("descricao").getText());
            conclusion.setCertaintyFactor(Double.parseDouble(ruleElement.getChild("conclusao").getChild("FC").getText()));
            
            theRule.setConclusion(conclusion);
            
            ruleList.add(theRule);
        }
        return ruleList;
    }
    
    public void removeRules() throws JDOMException, IOException {
        SAXBuilder builder = new SAXBuilder();
        
        this.doc = builder.build(new File(XML_FILE_PATH));
        doc.getRootElement().removeContent();
        saveDocument(this.doc);
    }
}